package com.lanyan.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;
        import com.lanyan.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.mapper.ProductMapper;
import com.lanyan.system.domain.Product;
import com.lanyan.system.domain.vo.ProductVo;
import com.lanyan.system.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 商品Service业务层处理
 *
 * @author lanyan
 * @date 2024-11-26
 */
@Service
public class ProductServiceImpl  extends ServiceImpl<ProductMapper,Product> implements IProductService

{
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询商品
     *
     * @param productId 商品主键
     * @return 商品
     */
    @Override
    public Product selectProductByProductId(Long productId)
    {
        return this.getById(productId);
    }

    /**
     * 查询商品Vo列表
     *
     * @param product 商品
     * @return 商品
     */
    @Override
    public List<ProductVo> selectProductVoList(Product product)
    {
        return selectProductList(product)
                .stream()
                .map(x -> (ProductVo)x)
                .collect(Collectors.toList());
    }
    /**
     * 查询商品列表
     *
     * @param product 商品
     * @return 商品
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        LambdaQueryChainWrapper<Product> wrapper = this.lambdaQuery().eq(Product::getDelFlag,"0");
        if (StringUtils.isNotEmpty(product.getName())) {
            wrapper.like(Product::getName, product.getName());
        }
        if (StringUtils.isNotEmpty(product.getMainImg())) {
            wrapper.eq(Product::getMainImg, product.getMainImg());
        }
        if (StringUtils.isNotEmpty(product.getSwiperImg())) {
            wrapper.eq(Product::getSwiperImg, product.getSwiperImg());
        }
        if (product.getTopClassifyId() != null) {
            wrapper.eq(Product::getTopClassifyId, product.getTopClassifyId());
        }
        if (product.getClassifyId() != null) {
            wrapper.and(w -> w.eq(Product::getClassifyId, product.getClassifyId())
                    .or().eq(Product::getClassifyIdOne, product.getClassifyId())
                    .or().eq(Product::getClassifyIdTwo, product.getClassifyId()));
        }
        if (StringUtils.isNotEmpty(product.getPrice())) {
            wrapper.eq(Product::getPrice, product.getPrice());
        }
        if (product.getMarketPrice() != null) {
            wrapper.eq(Product::getMarketPrice, product.getMarketPrice());
        }
        if (product.getStock() != null) {
            wrapper.eq(Product::getStock, product.getStock());
        }
        if (product.getSales() != null) {
            wrapper.eq(Product::getSales, product.getSales());
        }
        if (StringUtils.isNotEmpty(product.getDetails())) {
            wrapper.eq(Product::getDetails, product.getDetails());
        }
        if (StringUtils.isNotEmpty(product.getIsCommend())) {
            wrapper.eq(Product::getIsCommend, product.getIsCommend());
        }
        if (StringUtils.isNotEmpty(product.getIsMs())) {
            wrapper.eq(Product::getIsMs, product.getIsMs());
        }
        if (StringUtils.isNotEmpty(product.getMate())) {
            wrapper.eq(Product::getMate, product.getMate());
        }
        if (StringUtils.isNotEmpty(product.getIsSale())) {
            wrapper.eq(Product::getIsSale, product.getIsSale());
        }
        if (product.getProductSort()!= null) {
            wrapper.eq(Product::getProductSort, product.getProductSort());
        }
        wrapper.orderByDesc(Product::getProductSort);
        return wrapper.list();
    }

        /**
         * 新增商品
         *
         * @param product 商品
         * @return 结果
         */
    @Override
        public boolean insertProduct(Product product)
        {
                    product.setCreateTime(DateUtils.getNowDate());
                return this.save(product);
        }

        /**
         * 修改商品
         *
         * @param product 商品
         * @return 结果
         */
    @Override
        public boolean updateProduct(Product product)
        {
                    product.setUpdateTime(DateUtils.getNowDate());
            return this.updateById(product);
        }

        /**
         * 批量删除商品
         *
         * @param productIds 需要删除的商品主键
         * @return 结果
         */
    @Override
        public boolean deleteProductByProductIds(Long[] productIds)
        {
            return this.lambdaUpdate().set(Product::getDelFlag,"1").in(Product::getProductId,productIds).update();
        }

        /**
         * 删除商品信息
         *
         * @param productId 商品主键
         * @return 结果
         */
    @Override
        public boolean deleteProductByProductId(Long productId)
        {
            return this.lambdaUpdate().set(Product::getDelFlag,"1").eq(Product::getProductId,productId).update();
        }

    @Override
    public List<Product> getSum(String startTime, String endTime) {
        return productMapper.getSum(startTime,endTime);
    }

    @Override
    public List<Product> selectProductListClass(Product product) {

        LambdaQueryChainWrapper<Product> wrapper = this.lambdaQuery().eq(Product::getDelFlag,"0");
        if (StringUtils.isNotEmpty(product.getName())) {
            wrapper.like(Product::getName, product.getName());
        }
        if (StringUtils.isNotEmpty(product.getMainImg())) {
            wrapper.eq(Product::getMainImg, product.getMainImg());
        }
        if (StringUtils.isNotEmpty(product.getSwiperImg())) {
            wrapper.eq(Product::getSwiperImg, product.getSwiperImg());
        }
        if (product.getClassIdList() != null) {
            wrapper.and(w -> w.in(Product::getClassifyId, product.getClassIdList())
                    .or().in(Product::getClassifyIdOne, product.getClassIdList())
                    .or().in(Product::getClassifyIdTwo, product.getClassIdList()));
        }
        if (StringUtils.isNotEmpty(product.getPrice())) {
            wrapper.eq(Product::getPrice, product.getPrice());
        }
        if (product.getMarketPrice() != null) {
            wrapper.eq(Product::getMarketPrice, product.getMarketPrice());
        }
        if (product.getStock() != null) {
            wrapper.eq(Product::getStock, product.getStock());
        }
        if (product.getSales() != null) {
            wrapper.eq(Product::getSales, product.getSales());
        }
        if (StringUtils.isNotEmpty(product.getDetails())) {
            wrapper.eq(Product::getDetails, product.getDetails());
        }
        if (StringUtils.isNotEmpty(product.getIsCommend())) {
            wrapper.eq(Product::getIsCommend, product.getIsCommend());
        }
        if (StringUtils.isNotEmpty(product.getIsMs())) {
            wrapper.eq(Product::getIsMs, product.getIsMs());
        }
        if (StringUtils.isNotEmpty(product.getMate())) {
            wrapper.eq(Product::getMate, product.getMate());
        }
        if (StringUtils.isNotEmpty(product.getIsSale())) {
            wrapper.eq(Product::getIsSale, product.getIsSale());
        }
        if (product.getProductSort()!= null) {
            wrapper.eq(Product::getProductSort, product.getProductSort());
        }
        wrapper.orderByDesc(Product::getProductSort);
        return wrapper.list();
    }
}
